package com.villaday.idf.activity;

import android.Manifest;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.AppContext;
import com.villaday.idf.R;
import com.villaday.idf.adapter.ChoseCityGVAdapter;
import com.villaday.idf.adapter.ChoseCityLVAdapter;
import com.villaday.idf.adapter3.ChooseCityAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.GridViewInScrollView;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.custom.SideBar;
import com.villaday.idf.model.AreaListCopyEntity;
import com.villaday.idf.model.AreaListEntity;
import com.villaday.idf.model.City;
import com.villaday.idf.model.PoPularCityEntity;
import com.villaday.idf.model.Surrounds_citiesEntity;
import com.villaday.idf.model3.AddressTypeEntity;
import com.villaday.idf.util.CacheUtil;
import com.villaday.idf.util.CharacterParser;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.DbCityUtils;
import com.villaday.idf.util.LogUtil;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.PinYinComparator;
import com.villaday.idf.util.ToastUtil;
import com.villaday.idf.util.ToolBarUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2017/3/14 0014.
 */
public class ChooseCityActity<T> extends BaseActivity implements OkHttpUtil.OnDataListener, View.OnClickListener, ChoseCityLVAdapter.TextOnClick {

    @BindView(R.id.toolbar_text)
    public Toolbar toolbar;
    @BindView(R.id.lv_search)
    public ListView listView;


    @BindView(R.id.sideBar)
    public SideBar sideBar;
    @BindView(R.id.dialog)
    public TextView dialog;
    @BindView(R.id.toolbar_back)
    ImageButton toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_next)
    TextView toolbarNext;
    @BindView(R.id.Line_view)
    View LineView;
    @BindView(R.id.rl_cc)
    RelativeLayout rlCc;
    @BindView(R.id.rv_cc)
    RecyclerView rvCc;
    @BindView(R.id.et_ccfh_search)
    EditText mEtSearch;
    @BindView(R.id.tv_cc_no)
    TextView tvCcNo;


    private LoadingDialog loadingDialog;
    private DialogFragment tipsDialog;
    private List<AreaListEntity.BodyEntity.DistrictsEntity> dataList = new ArrayList<>();


    List<AreaListCopyEntity> data = new ArrayList<>();
    private ChoseCityLVAdapter choseCityLVAdapter;
    private CharacterParser characterParser;
    private PinYinComparator pinyinComparator;
    private List<AreaListCopyEntity> areaListCopyEntity = new ArrayList<>();
    private List<AreaListCopyEntity> allData = new ArrayList<>();
    private List<AreaListCopyEntity> addData = new ArrayList<>();

    private TextView tvAddr;
    private GridViewInScrollView gvChoseCity;
    private ChoseCityGVAdapter choseCityGVAdapter;
    private PoPularCityEntity poPularCityEntity;
    private double longitude;//经度
    private double latitude;//纬度
    private String cityName;
    private LinearLayout llHead;
    private String cityId;
    private Gson gson = new GsonBuilder().create();
    private GridViewInScrollView gv_hotChoseCity;
    private ChoseCityGVAdapter hotChoseCityGVAdapter;
    private Surrounds_citiesEntity hot;
    private Surrounds_citiesEntity around;
    private DbCityUtils dbCityUtils;
    private ImageView imgDelete;
    private TagFlowLayout flHistory;
    private LinearLayout llDelete;
    private TextView tv_zhoubianChengshi;
    private TextView tv_hotChoseCity;
    private String mType = "5";//酒店：5 别墅：0 民宿： 1
    private ChooseCityAdapter mChooseCityAdapter;
    private List<AddressTypeEntity.BodyBean.CitiesBean> mCities;


    @Override
    protected int getViewResId() {
        return R.layout.actity_choose_city;
    }

    @Override
    protected void init() {
        dbCityUtils = DbCityUtils.getInstance();

        if (getIntent().getStringExtra("type") != null) {
            mType = getIntent().getStringExtra("type");
        }

        cityId = getIntent().getStringExtra("cityId");

        ToolBarUtil.initToolBar(toolbar, "选择城市", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        }, "取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEtSearch.setText("");
                sideBar.setVisibility(View.VISIBLE);
                PhoneUtil.showOrHide(ChooseCityActity.this, ChooseCityActity.this);
//                if (PhoneUtil.isShowing(ChooseCityActity.this)) {
//                    PhoneUtil.showOrHide(ChooseCityActity.this);
//                }
            }
        });
        toolbarNext.setVisibility(View.INVISIBLE);
        //实例化汉字转拼音类
        characterParser = CharacterParser.getInstance();
        pinyinComparator = new PinYinComparator();

        llHead = (LinearLayout) LayoutInflater.from(this)
                .inflate(R.layout.item_chose_city_first_head, null, false);
        tvAddr = (TextView) llHead.findViewById(R.id.tv_choseCity_addr);
        gvChoseCity = (GridViewInScrollView) llHead.findViewById(R.id.gv_choseCity);
        gv_hotChoseCity = (GridViewInScrollView) llHead.findViewById(R.id.gv_hotChoseCity);
        tv_zhoubianChengshi = (TextView) llHead.findViewById(R.id.tv_zhoubianChengshi);
        tv_hotChoseCity = (TextView) llHead.findViewById(R.id.tv_hotChoseCity);


        //热门城市
        hotChoseCityGVAdapter = new ChoseCityGVAdapter(this);
        gv_hotChoseCity.setAdapter(hotChoseCityGVAdapter);
        gv_hotChoseCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dbCityUtils.insertOrReplace(
                        hot.getBody().get(position).getCode(),
                        hot.getBody().get(position).getId(),
                        hot.getBody().get(position).getName(),
                        hot.getBody().get(position).getServerTime(),
                        hot.getBody().get(position).getShortCode(),
                        false,
                        System.currentTimeMillis());


                String areaName = hot.getBody().get(position).getName();
                String areaid = hot.getBody().get(position).getId();
                Intent intent = getIntent();
                intent.putExtra("cityName", areaName);
                intent.putExtra("cityId", areaid);
                setResult(RESULT_OK, intent);
                onBackPressed();
            }
        });

        // tvAddr.setOnClickListener(this);
        //周边城市

        choseCityGVAdapter = new ChoseCityGVAdapter(this);
        gvChoseCity.setAdapter(choseCityGVAdapter);
        listView.addHeaderView(llHead);
        choseCityLVAdapter = new ChoseCityLVAdapter(this, areaListCopyEntity);
        choseCityLVAdapter.setTextOnClick(this);
        listView.setAdapter(choseCityLVAdapter);
        gvChoseCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dbCityUtils.insertOrReplace(
                        around.getBody().get(position).getCode(),
                        around.getBody().get(position).getId(),
                        around.getBody().get(position).getName(),
                        around.getBody().get(position).getServerTime(),
                        around.getBody().get(position).getShortCode(),
                        false,
                        System.currentTimeMillis());
                String areaName = around.getBody().get(position).getName();
                String areaid = around.getBody().get(position).getId();
                Intent intent = getIntent();
                intent.putExtra("cityName", areaName);
                intent.putExtra("cityId", areaid);
                setResult(RESULT_OK, intent);
                onBackPressed();

            }
        });
        getLocation();
        sideBar.setTextView(dialog);

        //设置右侧SideBar触摸监听
        sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {

            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                int position = choseCityLVAdapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    listView.setSelection(position + 1);
                }
                if (dialog != null) {
                    dialog.setVisibility(View.VISIBLE);
                    dialog.setText(s);
                }

            }


        });

        //历史
        imgDelete = (ImageView) llHead.findViewById(R.id.img_delete);
        flHistory = (TagFlowLayout) llHead.findViewById(R.id.fl_history);
        llDelete = (LinearLayout) llHead.findViewById(R.id.ll_delete);


        imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbCityUtils.deleteAll();
                List<City> bournSearchhistories = dbCityUtils.queryDownAllOrderByDate();
                setData(flHistory, llDelete, (List<T>) bournSearchhistories, false);
            }
        });
        List<City> bournSearchhistories = dbCityUtils.queryDownAllOrderByDate();
        setData(flHistory, llDelete, (List<T>) bournSearchhistories, true);


        initAdapter();

        mEtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    rvCc.setVisibility(View.VISIBLE);
                    rlCc.setVisibility(View.GONE);
                    toolbarNext.setVisibility(View.VISIBLE);
                    initAddress(s.toString());
                } else {
                    rvCc.setVisibility(View.GONE);
                    rlCc.setVisibility(View.VISIBLE);
                    toolbarNext.setVisibility(View.GONE);
                }
            }
        });

        mEtSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mEtSearch.setSelected(true);
                    mEtSearch.setFocusable(true);
                    mEtSearch.setCursorVisible(true);
                    sideBar.setVisibility(View.INVISIBLE);
                } else {
                    PhoneUtil.showOrHide(ChooseCityActity.this, ChooseCityActity.this);
                    if (PhoneUtil.isShowing(ChooseCityActity.this)) {
                        PhoneUtil.showOrHide(ChooseCityActity.this);
                    }
                    sideBar.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void initAddress(String key) {
        Map<String, String> map = new HashMap<>();
        map.put("keyword", key);
        map.put("type", "7");
        LogUtil.jLog().e(Constant.URL.addressType + "  " + new Gson().toJson(map));
        OkHttpUtil.getJSON(this, Constant.URL.addressType, map, new OkHttpUtil.OnDataListener() {

            @Override
            public void onResponse(String url, String json) {
                try {
                    LogUtil.jLog().e(json);
                    AddressTypeEntity addressTypeEntity = new Gson().fromJson(json, AddressTypeEntity.class);
                    if (addressTypeEntity.getCode().equals(Constant.Strings.SUC) && addressTypeEntity.getBody().getCities().size() > 0) {
                        tvCcNo.setVisibility(View.GONE);
                        rvCc.setVisibility(View.VISIBLE);
                        mCities = addressTypeEntity.getBody().getCities();
                        mChooseCityAdapter.setData(mCities);
                    } else {
                        if (mCities != null) {
                            mCities.clear();
                        }
                        mChooseCityAdapter.notifyDataSetChanged();
                        tvCcNo.setVisibility(View.VISIBLE);
                        rvCc.setVisibility(View.GONE);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });
    }


    private void initAdapter() {
        rvCc.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mChooseCityAdapter = new ChooseCityAdapter(this);
        mChooseCityAdapter.setOnItemClickListener(new AbsRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                dbCityUtils.insertOrReplace(
                        mCities.get(position).getCode(),
                        mCities.get(position).getId() + "",
                        mCities.get(position).getName(),
                        System.currentTimeMillis(),
                        "",
                        false,
                        System.currentTimeMillis());
                String areaName = mCities.get(position).getName();
                String areaid = mCities.get(position).getId() + "";
                Intent intent = getIntent();
                intent.putExtra("cityName", areaName);
                intent.putExtra("cityId", areaid);
                setResult(RESULT_OK, intent);
                onBackPressed();
            }
        });
        rvCc.setAdapter(mChooseCityAdapter);
    }


    private void getLocation() {
        LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
        }
        cityName = getCityName(longitude, latitude);
        tvAddr.setText("当前定位城市：" + AppContext.CityName);

    }

    public String getCityName(double longitude, double latitude) {

        String mcityName = null;
        Geocoder geocoder = new Geocoder(this);
        List<Address> addList = null;// 解析经纬度
        try {
            addList = geocoder.getFromLocation(latitude, longitude, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (addList != null && addList.size() > 0) {
            for (int i = 0; i < addList.size(); i++) {
                Address add = addList.get(i);
                mcityName = add.getLocality();
            }
            return mcityName;
        }


        return "无法定位";

    }

    @Override
    protected void loadData() {
        loadingDialog = LoadingDialog.newInstance("获取地址数据中...");
        loadingDialog.show(getFragmentManager());
        chooseAddress();
       /* if (PhoneUtil.isUrlCacheValid(this, Constant.URL.GetAreaList)) {//本地数据可用
            chooseAddress();
            //获取原本数据
            String areaString = CacheUtil.getUrlCache(Constant.URL.GetAreaList);
            if (!TextUtils.isEmpty(areaString)) {//读取到缓存
                getCopyData(areaString);


            }
        } else {
            CacheUtil.deleteUrlCache(Constant.URL.GetAreaList);

            OkHttpUtil.postJson(Constant.URL.GetAreaList, "0", this);
        }*/
       /* //热点城市
        if (PhoneUtil.isUrlCacheValid(this, Constant.URL.GetPopularCitys)) {//本地数据可用
            //chooseAddress();
            //获取原本数据
            String areaString = CacheUtil.getUrlCache(Constant.URL.GetPopularCitys);
            if (!TextUtils.isEmpty(areaString)) {//读取到缓存
                String decrypt = DesUtil.decrypt(areaString);
                poPularCityEntity = new Gson().fromJson(decrypt, PoPularCityEntity.class);
                choseCityGVAdapter.setData(poPularCityEntity.getData());

            }
        } else {
            CacheUtil.deleteUrlCache(Constant.URL.GetPopularCitys);
            OkHttpUtil.getJSON(this,Constant.URL.recommends_cities, this);
        }*/
    }

    private void chooseAddress() {
        OkHttpUtil.getJSON(this, Constant.URL.resources_cities, this);
    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.resources_cities.equals(url)) {//获取地址数据
                LogeUtil.e("resources_cities", json);

                AreaListEntity area = new Gson().fromJson(json, AreaListEntity.class);
                if ("200".equals(area.getCode()) && area.getBody() != null && area.getBody().getDistricts() != null) {

                    getCopyData(json);
                    //CacheUtil.setUrlCache(this, Constant.URL.GetAreaList, json);
                    Map<String, String> map = new HashMap<>();
                    map.put("type", mType);
                    OkHttpUtil.getJSON(this, Constant.URL.recommends_cities, map, this);


                }
            } else if (Constant.URL.recommends_cities.equals(url)) {
                LogUtil.jLog().e(json);
                LogeUtil.e("recommends_cities", json);
                Map<String, String> map = new HashMap<>();
                map.put("type", mType);
                OkHttpUtil.getJSON(this, String.format(Constant.URL.surrounds_cities, cityId), map, this);
                hot = gson.fromJson(json, Surrounds_citiesEntity.class);
                if ("200".equals(hot.getCode()) && hot.getBody() != null) {

                    hotChoseCityGVAdapter.setData(hot.getBody());

                } else {
                    gv_hotChoseCity.setVisibility(View.GONE);
                    tv_hotChoseCity.setVisibility(View.GONE);

                }
                if (hot.getBody() != null && hot.getBody().size() > 0) {

                } else {
                    gv_hotChoseCity.setVisibility(View.GONE);
                    tv_hotChoseCity.setVisibility(View.GONE);
                }

            } else if (String.format(Constant.URL.surrounds_cities, cityId).equals(url)) {
                dismissLoading();
                LogeUtil.e("surrounds_cities", json);

                around = gson.fromJson(json, Surrounds_citiesEntity.class);

                if (around != null && around.getBody() != null && "200".equals(around.getCode())) {
                    CacheUtil.setUrlCache(this, Constant.URL.GetPopularCitys, json);
                    //chooseAddress();

                    choseCityGVAdapter.setData(around.getBody());

                } else {
                    tv_zhoubianChengshi.setVisibility(View.GONE);
                    gvChoseCity.setVisibility(View.GONE);
                }

                if (around.getBody() != null && around.getBody().size() > 0) {

                } else {
                    tv_zhoubianChengshi.setVisibility(View.GONE);
                    gvChoseCity.setVisibility(View.GONE);
                }

            }
        }
    }

    private void getCopyData(String areaString) {
        AreaListEntity area = new Gson().fromJson(areaString, AreaListEntity.class);
        dataList = area.getBody().getDistricts();
        //处理数据并排序

        for (int i = 0; i < dataList.size(); i++) {
            List<AreaListEntity.BodyEntity.DistrictsEntity.CitiesEntity> chirldData = dataList.get(i).getCities();
            if (chirldData != null) {

                for (int j = 0; j < chirldData.size(); j++) {
                    // String areaName = chirldData.get(j).getName();
                    String firstChar = dataList.get(i).getName();
                    areaListCopyEntity.add(new AreaListCopyEntity(chirldData.get(j).getId(), chirldData.get(j).getName(), firstChar));

                }

            }
        }
        Collections.sort(areaListCopyEntity, pinyinComparator);
        choseCityLVAdapter.notifyDataSetChanged();

        allData.addAll(areaListCopyEntity);
    }


    @Override
    public void onFailure(String url, String error) {

    }

    private void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
        if (tipsDialog != null) {
            tipsDialog.dismiss();
        }
    }

    private String handleData(String name) {

        //汉字转换成拼音
        String pinyin = characterParser.getSelling(name);
        String s = pinyin.toUpperCase();
        return s.toUpperCase();
        //String sortString = pinyin.substring(0, 1).toUpperCase();
        //判断首字母是否是英文字母
       /* if (sortString.matches("[A-Z]")) {
            return sortString.toUpperCase();
        } else {
            return "#";
        }*/
    }

    /*@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String areaName = areaListCopyEntity.get(position).getAreaName();
        String areaId = areaListCopyEntity.get(position).getAreaId();

        Intent intent = new Intent(this, StoreFragment.class);
        intent.putExtra("areaName", areaName);
        intent.putExtra("areaId", areaId);
        setResult(1, intent);
        longitude = 0;
        latitude = 0;
        onBackPressed();
    }*/

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_searchBack://后退
                onBackPressed();
                break;

            case R.id.tv_choseCity_addr://点击热门城市
                if (!((TextView) v).getText().equals("当前位置：无法定位")) {
                    for (int i = 0; i < allData.size(); i++) {
                        if (((TextView) v).getText().equals(allData.get(i).getAreaName())) {
                           /* Intent intent = new Intent(this, StoreFragment.class);
                            intent.putExtra("areaName", allData.get(i).getAreaName());
                            intent.putExtra("areaId", allData.get(i).getAreaId());
                            setResult(1, intent);*/
                            break;
                        }
                    }
                }
                onBackPressed();
                break;
        }
    }


    public void hasFullChar(String str) {
        //输入英文
        if (str.getBytes().length == str.length()) {
            for (int i = 0; i < areaListCopyEntity.size(); i++) {
                if (areaListCopyEntity.get(i).getFirstChar().contains(str.toUpperCase())) {
                    addData.add(areaListCopyEntity.get(i));
                }
            }

            if (addData.size() == 0) {
                ToastUtil.initToast(this, "没有相关数据");
            } else {
                listView.removeHeaderView(llHead);
                areaListCopyEntity.clear();
                areaListCopyEntity.addAll(addData);
                choseCityLVAdapter.notifyDataSetChanged();
                //areaListCopyEntity.clear();
                //areaListCopyEntity.addAll(allData);
            }

        } else {//输入中文
            for (int i = 0; i < areaListCopyEntity.size(); i++) {
                if (areaListCopyEntity.get(i).getAreaName().contains(str)) {
                    addData.add(areaListCopyEntity.get(i));
                }
            }

            if (addData.size() == 0) {
                ToastUtil.initToast(this, "没有相关数据");
            } else {
                listView.removeHeaderView(llHead);
                areaListCopyEntity.clear();
                areaListCopyEntity.addAll(addData);
                choseCityLVAdapter.notifyDataSetChanged();

            }
            return;
        }
    }


    @Override
    public void textOnClick(int position) {
        dbCityUtils.insertOrReplace(
                "",
                areaListCopyEntity.get(position).getAreaId(),
                areaListCopyEntity.get(position).getAreaName(),
                0,
                "0",
                false,
                System.currentTimeMillis());
        String areaName = areaListCopyEntity.get(position).getAreaName();
        String areaId = areaListCopyEntity.get(position).getAreaId();
        Intent intent = getIntent();
        intent.putExtra("cityName", areaName);
        intent.putExtra("cityId", areaId);
        setResult(RESULT_OK, intent);
        onBackPressed();

    }

    private void setData(final TagFlowLayout fl, LinearLayout ll, final List<T> object, boolean isRetract) {

        if (object == null || object.size() == 0) {
            ll.setVisibility(View.GONE);

        } else {
            ll.setVisibility(View.VISIBLE);

        }
        if (object != null) {
            // ll.setVisibility(View.VISIBLE);
            TagAdapter<T> tagAdapter = new TagAdapter<T>(object) {
                @Override
                public View getView(FlowLayout parent, int position, T s) {
                    TextView tv = (TextView) getLayoutInflater().inflate(R.layout.tv_h,
                            fl, false);
                    if (object.get(0) instanceof City) {
                        //搜索
                        City hot = (City) s;

                        if (hot.getName().length() > 10) {
                            tv.setText(hot.getName().substring(0, 9) + "...");

                        } else {
                            tv.setText(hot.getName());

                        }
                    }


                    return tv;
                }
            };
            fl.setAdapter(tagAdapter);
            fl.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    if (object.get(0) instanceof City) {//搜索
                        List<City> hot = (List<City>) object;
                        String areaName = hot.get(position).getName();
                        String areaid = hot.get(position).getCitrId();
                        Intent intent = getIntent();
                        intent.putExtra("cityName", areaName);
                        intent.putExtra("cityId", areaid);
                        setResult(RESULT_OK, intent);
                        onBackPressed();
                    }

                    return true;
                }
            });
        } else {
            //ll.setVisibility(View.GONE);

        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}